﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ooaavee.TestTools;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Ooaavee.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using Ooaavee.Data.Model;
using Ooaavee.Support;
using Ooaavee.Data.Queries;

namespace UnitTests.Ooaavee.Data
{
    [TestClass]
    public class RelationalModelQueriesTest : TestBase
    {

        #region Initialize tests

        private RelationalModelSource Source { set; get; }
        private RelationalModel Model { set; get; }
        private QueryContext Context { set; get; }

        protected override void OnBeforeTest(TestMethodEventArgs e)
        {
            const string connectionsString = @"Provider=SQLOLEDB;Data Source=localhost\SQLEXPRESS;Initial Catalog=AdventureWorks2012;Integrated Security=SSPI;";
            Source = new RelationalModelSource { OleDbConnectionString = connectionsString };

            Model = RelationalModelLoader.Load(Source);

            Context = QueryContext.CreateQueryContext(Source, Model);

            base.OnBeforeTest(e);
        }

        #endregion

        [TestMethod]
        public void SerializeTest()
        {
            var serialized1 = Serializer.SerializeDataContract(Model);
            var deserialized = Serializer.DeserializeDataContract<RelationalModel>(serialized1);
            var serialized2 = Serializer.SerializeDataContract(deserialized);

            Assert.IsNotNull(serialized1);
            Assert.IsNotNull(deserialized);
            Assert.AreEqual(serialized1, serialized2);
        }

        [TestMethod]
        public void SelectAllUserTablesAndUserViewsTest()
        {
            List<Table> tables = new List<Table>();
            tables.AddRange(Model.UserTables);
            tables.AddRange(Model.UserViews);

            foreach (Table table in tables)
            {
                QueryResult result = table.Select();

                foreach (QueryResultRow row in result.Rows)
                {
                    foreach (Dependency dependency in row.Dependencies)
                    {
                        var x = dependency.Data;

                    }   
                }
            }
        }
    }
}
